<template>
    <div>
        <custom-table
            autoResize
            rowKey="id"
            size="small"
            :loading="loading"
            :columns="tableColumns"
            :data-source="list"
            :pagination="paginationConfig"
            :expandIcon="handleExpandIcon"
            @change="handleTableChange"
            @expand="onExpandChange"
        >
            <template #customSystemTitle>
                <span style="margin-left: 20px">所属产品</span>
            </template>
            <template #systemName="{ record }">
                <router-link
                    :to="{ path: '/productAnalysis/productDetail', query: { id: record.requestSystem } }"
                    :style="{ marginLeft: (+record.stage >= 4 && record.isSplit === '是') || record.relateRequest > 0 ? 0 : '20px' }"
                    v-if="record.systemName && !record.systemName.includes(',')"
                >
                    {{ record.systemName }}
                </router-link>
                <span :style="{ marginLeft: (+record.stage >= 4 && record.isSplit === '是') || record.relateRequest > 0 ? 0 : '20px' }" v-else>{{
                    record.systemName
                }}</span>
            </template>
            <template #requestName="{ record }">
                <a @click.stop="showDetail(record.id)">
                    {{ record.requestName }}
                </a>
                <a-tag v-if="record.requireChange === 0" color="orange" class="margin-l-8">需求变更</a-tag>
            </template>
            <template #pmpLabels="{ record }">
                <a-tag color="blue" style="max-width: 100%; margin-bottom: 2px" v-fitText v-for="item of record.pmpLabels" :key="item.id">
                    {{ item.name }}
                </a-tag>
            </template>
            <template #requestType="{ record }"> {{ $filters.valToNameFilter('requestType', record.requestType) }} </template>
            <template #level="{ record }">
                <h-level :level="record.level" v-if="record.level" />
            </template>
            <template #stage="{ record }"> {{ $filters.valToNameFilter('stage', record.stage) }} </template>
            <template #status="{ record }">
                <h-status-badge :status="record.status" />
            </template>
            <template #initiatorTime="{ record }"> {{ $filters.dateFilter(record.initiatorTime) }} </template>
            <template #estimateTime="{ record }"> {{ $filters.dateFilter(record.estimateTime) }} </template>
            <template #action="{ record }">
                <TableAction :record="record" @actionEvent="handleMenuEvent" />
            </template>
        </custom-table>
        <relative-change-table ref="relativeChangeTableRef" />
    </div>
</template>

<script lang="ts">
import store from '@/store';
import { HLevel, HStatusBadge, CustomTable } from '@/components';
import RelativeChangeTable from '@/components/Content/RelativeChangeTable.vue';
import TableAction from './tableAction.vue';
import { useSystemSet } from '@/components/SysSet/hooks/useSystemSet';
import { useMountedAndActivated } from '@/hooks/common/useMountedAndActivated';
import {
    useTableData,
    useTableLoading,
    usePagination,
    useTableColumns,
    useTableExpand,
    useTableFormSearch,
    useTableAction,
    useTableToolbar
} from '@/components/Table/hooks/index';
import { defineComponent, ref } from 'vue';
export default defineComponent({
    name: 'TableMode',
    components: {
        HLevel,
        RelativeChangeTable,
        TableAction,
        CustomTable,
        HStatusBadge
    },
    setup() {
        const relativeChangeTableRef = ref();
        const { loading, setLoading } = useTableLoading();
        const { paginationConfig, setPaginationConfig } = usePagination();
        const { columns: tableColumns, setColumns, settingColumns } = useTableColumns('myTaskRequirements');
        const { list, searchQuery, getList, setList, handleTableChange, setSearchQuery } = useTableData(
            setLoading,
            paginationConfig,
            setPaginationConfig,
            'taskRequirements'
        );
        const { handleExpandIcon, onExpandChange } = useTableExpand('sonRequestInfos', list, setList);
        const { handleSearch } = useTableFormSearch(setPaginationConfig, setSearchQuery, getList);
        const { showDetail, handleDelete, showRequireChangeDetail, handleChangeRequire } = useTableAction(
            'taskRequirements',
            getList,
            relativeChangeTableRef
        );
        const { exportAllFile } = useTableToolbar(searchQuery, paginationConfig);
        const { system } = useSystemSet(setPaginationConfig, setSearchQuery, getList);
        // 操作回调
        const handleMenuEvent = ({ type, record }) => {
            const { id, requestName } = record;
            switch (type) {
                case 'detail':
                    showDetail(id);
                    return;
                case 'showChange':
                    showRequireChangeDetail(id, 0);
                    return;
                case 'change':
                    handleChangeRequire(id);
                    return;
                case 'delete':
                    handleDelete(id, requestName);
                    return;
            }
        };
        // 单选切换回调;
        const radioChange = (e) => {
            setPaginationConfig({
                current: 1
            });
            setSearchQuery({
                status: e.target.value
            });
            getList();
        };
        setSearchQuery({
            stageOrStatus: 'status',
            status: null,
            admin: store.getters.userInfo.roles.some((item) => ['admin', '观察者', '领导', '产品管理委员会'].includes(item)) ? 1 : 0
        });

        const sysSetChange = (checkedSystem = null) => {
            setPaginationConfig({
                current: 1
            });
            setSearchQuery({
                system: checkedSystem,
                requestSystem: checkedSystem // 业务需求的system
            });
            system.value = checkedSystem;
            getList();
        };

        useMountedAndActivated(getList, false);
        return {
            relativeChangeTableRef,
            handleMenuEvent,
            list,
            tableColumns,
            settingColumns,
            loading,
            handleExpandIcon,
            paginationConfig,
            handleTableChange,
            onExpandChange,
            showDetail,
            handleSearch,
            radioChange,
            exportAllFile,
            setColumns,
            system,
            sysSetChange,
            getList
        };
    }
});
</script>

<style scoped></style>
